import subprocess


def read_commit_message(file_path):
    """读取提交消息文件的全部内容"""
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            return file.read().strip()
    except FileNotFoundError:
        print(f"Error: {file_path} not found.")
        return None


def run_git_commands(commit_message):
    """运行 Git 命令"""
    if commit_message:
        try:
            # 执行 git add .
            subprocess.run(['git', 'add', '.'], check=True)

            # 执行 git commit -m "{commit_message}"
            subprocess.run(['git', 'commit', '-m', commit_message], check=True)

            # 执行 git push
            subprocess.run(['git', 'push'], check=True)

        except subprocess.CalledProcessError as e:
            print(f"Error: {e}")
    else:
        print("No commit message provided.")


if __name__ == "__main__":
    commit_message_file = 'git-commit.txt'
    commit_message = read_commit_message(commit_message_file)
    run_git_commands(commit_message)